package com.example.myapp.ADO;

import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.example.myapp.Activity.Friendshome;
import com.example.myapp.Activity.Login;
import com.example.myapp.Activity.Myhome;
import com.example.myapp.Activity.Showimage;
import com.example.myapp.Loading.ShapeLoadingDialog;
import com.example.myapp.Model.Record;
import com.example.myapp.Model.User;
import com.example.myapp.R;
import com.loopj.android.image.SmartImageView;

import java.util.List;

import cn.bmob.v3.BmobUser;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.UpdateListener;
import de.hdodenhof.circleimageview.CircleImageView;

public class RecordAdopter  extends BaseAdapter {

    private Context context;
    private List<Record> recordList;
    private LayoutInflater mInflater;
    private ShapeLoadingDialog shapeLoadingDialog;
    private ListView lsitnew;
    //自定义adaper开始
    public RecordAdopter(Context context, List<Record> recordList) {
        this.context = context;
        this.recordList = recordList;

        mInflater = LayoutInflater.from(context);//初始化

    }
    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 1:{
                    Record p2 = new Record();
                    p2.setObjectId((String)msg.obj);
                    p2.delete(new UpdateListener() {
                        @Override
                        public void done(BmobException e) {
                            if(e==null){
                                Log.w("aaaa","失物招领招删除成功");

                            }else{
                                Log.w("aaaa","删除失败：" + e.getMessage());
                            }
                        }

                    });
                }break;
            }
        }
    };


    @Override
    public boolean areAllItemsEnabled() {
        return super.areAllItemsEnabled();
    }

    @Override
    public int getCount() {
        return recordList.size();
    }

    @Override
    public Object getItem(int i) {
        return recordList.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        view = mInflater.inflate(R.layout.recorditem, null);
        CircleImageView icon = view.findViewById(R.id.record_icon);
        TextView username = view.findViewById(R.id.record_username);
        TextView record_time = view.findViewById(R.id.record_time);
        TextView record_content = view.findViewById(R.id.record_content);
        SmartImageView record_image = view.findViewById(R.id.record_image);
        TextView record_place = view.findViewById(R.id.record_place);
        TextView record_tel = view.findViewById(R.id.record_tel);
        LinearLayout record_imagelayout = view.findViewById(R.id.record_imagelayout);
        TextView record_clear = view.findViewById(R.id.record_clear);
        record_time.setText(recordList.get(i).getUpdatedAt());
        username.setText(recordList.get(i).getAuthor().getUsername());
        if(recordList.get(i).getAuthor().getIcon()!=null){
            Glide.with(context).load(recordList.get(i).getAuthor().getIcon().getUrl()).into(icon);// CircleImageView.显示网络图片特殊方法
        }

        record_content.setText(recordList.get(i).getContent());
        record_place.setText(recordList.get(i).getPlace());
        record_tel.setText(recordList.get(i).getTel());
        /**初始化照片*/
        if (recordList.get(i).getImage() == null) {
            record_imagelayout.setVisibility(View.GONE);
        } else {
            record_image.setImageUrl(recordList.get(i).getImage().getUrl());
        }
        if(BmobUser.isLogin()){
            if(recordList.get(i).getAuthor().getObjectId().equals(BmobUser.getCurrentUser(User.class).getObjectId())){
                record_clear.setVisibility(View.VISIBLE);
            }
        }

        icon.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(!BmobUser.isLogin()){
                    Toast.makeText(context, "请登陆后再操作！", Toast.LENGTH_SHORT).show();
                    return;
                }
                if ( recordList.get(i).getAuthor().getObjectId().equals(BmobUser.getCurrentUser(User.class).getObjectId())){
                    Intent intent = new Intent(context, Myhome.class);
                    context.startActivity(intent);
                }else {
                    Intent intent = new Intent(context, Friendshome.class);
                    intent.putExtra("userID", recordList.get(i).getAuthor().getObjectId());
                    intent.putExtra("iconUrl",recordList.get(i).getAuthor().getIcon().getUrl());
                    intent.putExtra("userName",recordList.get(i).getAuthor().getUsername());
                    if (recordList.get(i).getAuthor().getShow()!=null){
                        intent.putExtra("jianJie",recordList.get(i).getAuthor().getShow());
                    }
                    context.startActivity(intent);
                }
            }
        });
        record_image.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, Showimage.class);
                intent.putExtra("imagepath", recordList.get(i).getImage().getUrl());
                context.startActivity(intent);
            }
        });//放大图片
        record_clear.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                View view = mInflater.inflate(R.layout.exit_dialog_layout, null);
                MyDialog mMyDialog = new MyDialog(context, 0, 0, view, R.style.DialogTheme);
                TextView exitall = view.findViewById(R.id.exitall);
                TextView dialog_title = view.findViewById(R.id.dialog_title);
                TextView exitback = view.findViewById(R.id.exitback);
                dialog_title.setText("你确定删除吗？");
                mMyDialog.setCancelable(true);
                mMyDialog.show();
                exitall.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Message message = new Message();
                        message.what = 1;
                        message.obj=recordList.get(i).getObjectId();
                        handler.sendMessage(message);
                        mMyDialog.dismiss();
                        recordList.remove(i);
                        notifyDataSetChanged();

                    }
                });//取消关注
                exitback.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        mMyDialog.dismiss();
                    }
                });
            }
        });//删除

        return view;
    }


}
